package cn.com.dev;

import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import cn.com.kh.KHUtil;
import cn.com.kh.getProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @projectName: ngsapitask-sw
 * @package: cn.com.dev
 * @className: DevSZWGDECE
 * @author: LiJT
 * @description: TODO
 * @date: 2023-05-25 17:47
 * @version: 1.0
 */
public class DevSZWGDECE {
    private static final Log log = LogFactory.getLog(DevSZWGDECE.class);

    public static void main(String[] args) throws Exception {

        getCaseTasks(null);

    }

    // DE30010 案件流程任务同步
    public static boolean getCaseTasks(EliteBean eliteBean) throws Exception {

        String logPrefix = "DE30010 案件流程任务同步-";
        log.debug(logPrefix + "开始...");
        String Sync_Time = "2023-05-24 18:00:00";
        log.debug(logPrefix + "本次获取开始时间范围：" + Sync_Time);
        String requestId = "SW-" + KHUtil.getRandomString(29).toUpperCase();
        String time = String.valueOf(System.currentTimeMillis());

        //设置头文件
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");
        headMap.put("funCode", "DE30010");
        headMap.put("appId", "44030010");
        headMap.put("appKey", "123456");
        headMap.put("requestId", requestId);
        headMap.put("timestamp", time);
        headMap.put("encoding", "");

        JSONObject callJson = new JSONObject();
        callJson.put("deptId", "10116");
        callJson.put("updateTime", Sync_Time);

        log.debug(logPrefix + "调用地址URL：" + "https://smartum.sz.gov.cn/szcg/caseCenterApi/api/dataExchange" + "调用发送报文信息：" + callJson.toString());
        String reJson = Util.doHttpPostUseHead("https://smartum.sz.gov.cn/szcg/caseCenterApi/api/dataExchange", callJson.toString(), "UTF-8", 10000, 0, headMap);
        log.debug(logPrefix + "调用返回信息：" + reJson.toString());

        JSONObject inObject = new JSONObject(reJson);
        String errorCode = KHUtil.GetJV(inObject, "errorCode");
        String errorMsg = KHUtil.GetJV(inObject, "errorMsg");
        String data = KHUtil.GetJV(inObject, "data");

        if (errorCode.equals("0")) {

            long rstartTime = System.currentTimeMillis();
            long rendTime = System.currentTimeMillis();
            log.debug(logPrefix + "处理开始时间:" + rstartTime);

            try {
                JSONArray arrJsonArray = new JSONArray(data);

                int arrInt = arrJsonArray.length();
                log.debug(logPrefix + "返回Array数量:" + arrInt);

                for (int i = 0; i < arrInt; i++) {

                    JSONObject arrCustJson = arrJsonArray.getJSONObject(i);

                    // Array信息解析
                    String id = KHUtil.GetJV(arrCustJson, "id");
                    String evtId = KHUtil.GetJV(arrCustJson, "evtId");
                    String inDate = KHUtil.GetJV(arrCustJson, "inDate");
                    String finishedDate = KHUtil.GetJV(arrCustJson, "finishedDate");
                    String limit = KHUtil.GetJV(arrCustJson, "limit");
                    String activityInstId = KHUtil.GetJV(arrCustJson, "activityInstId");
                    String remark = KHUtil.GetJV(arrCustJson, "remark");
                    String jumpType = KHUtil.GetJV(arrCustJson, "jumpType");

                    if (evtId.endsWith("45505")) {
                        System.err.println(logPrefix + "Array信息解析：" + evtId + " inDate:" + KHUtil.stampToDate(inDate));
                    } else {
                        System.out.println(logPrefix + "Array信息解析：" + evtId + " inDate:" + KHUtil.stampToDate(inDate));
                    }

                }

                //生成处理完成时间
                rendTime = System.currentTimeMillis();

                // 包装拼接返回JSONObject
                log.debug(logPrefix + "处理完成、调用问题处理时长(毫秒):" + (rendTime - rstartTime));

            } catch (JSONException e) {
                rendTime = System.currentTimeMillis();
                log.error(logPrefix + "处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (rendTime - rstartTime));
            }
        }

        return true;

    }
}
